package com.aop.SpElAop.DistributedLock.RedisLock.support;

/**
 * @Package: com.aop.SpElAop.Lock
 * @ClassName: LockFactory
 * @Author: h2cloud
 * @CreateTime: 2024/7/9 10:08 下午
 * @Description:
 */

import com.aop.SpElAop.DistributedLock.Lock;
import com.aop.SpElAop.DistributedLock.RedisLock.LockType.FairLock;
import com.aop.SpElAop.DistributedLock.RedisLock.LockType.ReadLock;
import com.aop.SpElAop.DistributedLock.RedisLock.LockType.ReentrantLock;
import com.aop.SpElAop.DistributedLock.RedisLock.LockType.WriteLock;
import com.aop.SpElAop.DistributedLock.RedisLock.RedisLock;
import org.aspectj.lang.JoinPoint;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class LockFactory {

	@Autowired
	private RedissonClient redissonClient;

	@Autowired
	private LockKeyProvider keyProvider;

	public Lock getLock(JoinPoint joinPoint, RedisLock redisLock) {
		LockInfo lockInfo = keyProvider.getInfo(joinPoint, redisLock);
		switch (lockInfo.getLockType()) {
			case REENTRANT:
				return new ReentrantLock(redissonClient, lockInfo);
			case FAIR:
				return new FairLock(redissonClient, lockInfo);
			case READ:
				return new ReadLock(redissonClient, lockInfo);
			case WRITE:
				return new WriteLock(redissonClient, lockInfo);
			default:
				throw new UnsupportedOperationException("还未实现该类型的锁");
		}
	}
}

